﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataCom;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Globalization;

namespace DataCommunication
{
    public class LineShiftDataAccess : ILineShiftDataAccess
    {
        /// <summary>
        /// Get Line Shift
        /// </summary>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public DataTable GetLineShift(string lineCode)
        {
            int rowAffected = 0;

            string connectionStr = ConfigurationManager.AppSettings["DBConnectionString"];

            SqlConnection connection = new SqlConnection(connectionStr);
            SqlCommand command = new SqlCommand();
            command.Connection = connection;

            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            dataAdapter.SelectCommand = command;

            connection.Open();

            if (string.IsNullOrWhiteSpace(lineCode))
            {
                command.CommandText = "select LineCode, Shift, StartTime, EndTime, RestTime, PlanNum,NumTime from dbo.LineShift";
            }
            else
            {
                command.CommandText = "select LineCode, Shift, StartTime, EndTime, RestTime, PlanNum,NumTime from dbo.LineShift where LineCode = @LineCode";

                command.Parameters.Clear();
                command.Parameters.AddWithValue("@LineCode", lineCode);
            }

            DataSet dsLineShift = new DataSet();
            dsLineShift.Locale = CultureInfo.InvariantCulture;
            rowAffected = dataAdapter.Fill(dsLineShift);

            command.Parameters.Clear();
            command.Dispose();

            if (connection.State != ConnectionState.Closed)
            {
                connection.Close();
            }

            return dsLineShift.Tables[0];
        }

        public DataTable GetLineShift_New(string lineCode,string NumTime)
        {
            int rowAffected = 0;

            string connectionStr = ConfigurationManager.AppSettings["DBConnectionString"];

            SqlConnection connection = new SqlConnection(connectionStr);
            SqlCommand command = new SqlCommand();
            command.Connection = connection;

            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            dataAdapter.SelectCommand = command;

            connection.Open();

            if (string.IsNullOrWhiteSpace(lineCode))
            {
                command.CommandText = "select LineCode, Shift, StartTime, EndTime, RestTime, PlanNum,NumTime from dbo.LineShift";
            }
            else
            {
                command.CommandText = "select LineCode, Shift, StartTime, EndTime, RestTime, PlanNum,NumTime from dbo.LineShift where LineCode = @LineCode and NumTime=@NumTime";

                command.Parameters.Clear();
                command.Parameters.AddWithValue("@LineCode", lineCode);
                command.Parameters.AddWithValue("@NumTime", NumTime);
            }

            DataSet dsLineShift = new DataSet();
            dsLineShift.Locale = CultureInfo.InvariantCulture;
            rowAffected = dataAdapter.Fill(dsLineShift);

            command.Parameters.Clear();
            command.Dispose();

            if (connection.State != ConnectionState.Closed)
            {
                connection.Close();
            }

            return dsLineShift.Tables[0];
        }

        public string GetLineNumTime(string lineCode)
        {
            string connectionStr = ConfigurationManager.AppSettings["DBConnectionString"];

            SqlConnection connection = new SqlConnection(connectionStr);
            SqlCommand command = new SqlCommand();
            command.Connection = connection;

            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            dataAdapter.SelectCommand = command;

            connection.Open();

            if (string.IsNullOrWhiteSpace(lineCode))
            {
                command.CommandText = "select NumTime from dbo.LineShift";
            }
            else
            {

                string DateTime_Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                command.CommandText = "select NumTime from dbo.LineShift where LineCode = '" + lineCode + "' and StartTime <='" + DateTime_Now + "' and EndTime>='" + DateTime_Now + "'";
                
            }
            
            command.Parameters.Clear();
            command.Dispose();

            if (connection.State != ConnectionState.Closed)
            {
                connection.Close();
            }

            DataTable dt = new DataTable();
            dataAdapter.Fill(dt);

            if (dt.Rows.Count != 0)
            {
                return dt.Rows[0][0].ToString();
            }
            else
            {
                return "0";
            }
        }
    }
}
